<template>
  <div class="search">
    <h1>活动管理</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>活动标题</span>
    <el-input
      v-model="Value"
      style="width: 200px; margin-right: 40%"
      placeholder="请输入"
    ></el-input>
    <el-button type="primary" @click="searchVehicleRegions">查询</el-button>
    <el-button type="default" @click="reset">重置</el-button>
  </div>
  <div class="page">
    <div class="page-header">
      <div class="page-header-left">
        <span>活动列表</span>
      </div>
      <div class="page-header-right">
        <el-button
          type="primary"
          style="margin-top: 1rem"
          @click="to('tenement/newActivity')"
          >+&nbsp;新建
        </el-button>
        <el-button type="default" style="margin-top: 1rem; margin-right: 2rem"
          >批量操作</el-button
        >
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" />
      <el-table-column property="title" label="活动标题" />
      <el-table-column property="author" label="作者" />
      <el-table-column label="发布状态">
        <template #default="{ row }">
          {{ row.anStatus === 1 ? "已发布" : "未发布" }}
        </template>
      </el-table-column>
      <el-table-column property="" label="浏览量" />
      <el-table-column property="publisher" label="发布人" />
      <el-table-column property="anDate" label="发布日期" />
      <el-table-column prop="detail" label="活动详情" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button @click="openDetailDialog(row.id)" type="text"
            >编辑
          </el-button>
          <el-button
            v-if="row.position_status === '审核中'"
            @click="approveVehiclePosition(row.id)"
            type="text"
            size="small"
            >审核通过
          </el-button>
          <el-button
            v-if="row.position_status === '审核中'"
            @click="rejectVehiclePosition(row.id)"
            type="text"
            size="small"
            >审核不通过
          </el-button>
          <el-button @click="deleteById(row.id)" type="text" size="small"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页操作-->
    <div class="pagination">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20]"
        style="margin-bottom: 2rem"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage, ElTable } from "element-plus";
import to from "@/utils/to.js";
import axios from "axios";
import router from "@/router";

const pageSizeOptions = [5, 10, 20];
const tableData = ref([]);
const totalCount = ref(0);
const pageNum = ref(1);
const pageSize = ref(pageSizeOptions[0]);
const position_status = ref("");
const Value = ref("");
const options = [
  { value: "", label: "全部" },
  { value: "审核中", label: "审核中" },
  { value: "审核通过", label: "审核通过" },
  { value: "审核不通过", label: "审核不通过" },
];

const searchVehicleRegions = async () => {
  try {
    const response = await axios.get(
      `http://localhost:8080/property/action/page-query`,
      {
        headers: {
          Authorization: localStorage.getItem("token"),
        },
        params: {
          page: pageNum.value,
          pageSize: pageSize.value,
          id: Value.value,
          title: Value.value,
          author: Value.value,
          anStatus: Value.value,
          publisher: Value.value,
          anDate: Value.value,
          detail: Value.value,
          status: position_status.value,
        },
      }
    );

    tableData.value = response.data.data.records;
    totalCount.value = response.data.data.total;
  } catch (error) {
    console.error("查询区域列表失败:", error);
  }
};

//hhh
const reset = () => {
  Value.value = ""; // 清空查询条件
  pageNum.value = 1; // 重置为第一页
  pageSize.value = pageSizeOptions[0]; // 重置每页条数为默认值
  searchVehicleRegions(); // 重新查询所有住户数据
};

const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  searchVehicleRegions();
};

const handleCurrentChange = (newPage) => {
  pageNum.value = newPage;
  searchVehicleRegions();
};
// 初始化加载
searchVehicleRegions();

//编辑操作
const openDetailDialog = (id) => {
  router.push({ path: "/backend/tenement/newActivity", query: { id: id } });
};
const deleteById = (id) => {
  const config = {
    url: `http://localhost:8080/property/action/remove/${id}`,
    method: "DELETE",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: "数据删除成功",
          type: "success",
        });
        setTimeout(() => {
          searchVehicleRegions();
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
</script>

<style scoped>
.search {
  background-color: white;
  padding-bottom: 2rem;
  height: auto;
  margin-top: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin-top: 2rem;
  margin-left: 3rem;
  margin-right: 1rem;
  height: 24px;
}

.page {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  display: flex;
  margin-top: 2rem;
  justify-content: center;
}
</style>
